

    AREA    |.text|,  CODE, READONLY
    
Stx     PROC
        EXPORT Stx
        
        MOV     R0, R14
        MOV     R1, #0X04
        ORRS    R0, R1 
        MOV     R14, R0
        
        ENDP
        
        ALIGN
; SVC 服务函数列表
Func_Vector
        DCD RETURN
        DCD MPU_Reg_Get     ;获取
        DCD Privilege_entry ;进入特权模式
        DCD Privilege_exit  ;退出特权模式
            
RETURN  PROC
        BX  LR
        ENDP
            
SVC_Handler PROC
        EXPORT SVC_Handler
;        MRS     R0, CONTROL
;        MOV     R1, #3
;        ORRS    R0, R1 
;        MSR     CONTROL, R0
;        MRS     R0,  MSP
;        MSR     R0,  PSP

        ; LDR     R0, =0XFFFFFFFD
        ; MOV     R14,    R0
        ; ORR     R14,    R14, #0X4
        TST     LR, #0x4
        ITE     EQ
        MRSEQ   R0, MSP
        MRSNE   R0, PSP
        
        LDR     R1, [R0, #24]
        LDRB    R0, [R1, #-2]
        MOV     R2, #4
        LDR     R1, =Func_Vector
        MLA     R1, R0, R2, R1
        LDR     R1, [R1]
        PUSH    {LR}
        BLX     R1
        POP     {LR}
        BX      LR
        ENDP
            
Stack_switch PROC
        ;主栈切换到线程栈，要求当前处于特权模式下
        IMPORT  __thread_sp
        EXPORT  Stack_switch
        LDR     R0, =__thread_sp
        MSR     PSP, R0
        MRS     R0, CONTROL
        ORR     R0, #0X3
        MSR     CONTROL, R0
        BX      LR
        ENDP

Privilege_entry PROC
        PUSH    {R0}
        MRS     R0, CONTROL
        AND     R0, #~0X3
        MSR     CONTROL,    R0
        POP     {R0}
        BX      LR
        ENDP
        
Privilege_exit  PROC
        PUSH    {R0}
        MRS     R0, CONTROL
        ORR     R0, #0X3
        MSR     CONTROL, R0
        POP     {R0}
        BX      LR
        ENDP
        

MPU_TYPE    EQU 0xE000ED90
MPU_CTRL    EQU 0xE000ED94
MPU_RNR     EQU 0xE000ED98
MPU_RBAR    EQU 0xE000ED9C
MPU_RASR    EQU 0xE000EDA0
MPU_RBAR_A1 EQU 0xE000EDA4
MPU_RASR_A1 EQU 0xE000EDA8
MPU_RBAR_A2 EQU 0xE000EDAC
MPU_RASR_A2 EQU 0xE000EDB0
MPU_RBAR_A3 EQU 0xE000EDB4
MPU_RASR_A3 EQU 0xE000EDB8

MPU_Reg_Get PROC
        PUSH    {R0-R12}
        LDR     R0, =MPU_TYPE
        LDM     R0, {R1-R11}
        POP     {R0-R12}
        BX      LR
        ENDP
        
        ALIGN
        END
